package com.ijiangtao.studevtech.demo.csv.day.DT20170501;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.CsvBeanWriter;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.io.ICsvBeanWriter;
import org.supercsv.prefs.CsvPreference;

import com.ijiangtao.studevtech.demo.csv.day.DT20170501.model.User;
import com.ijiangtao.studevtech.demo.csv.day.DT20170501.model.UserBean;
import com.ijiangtao.studevtech.demo.csv.day.DT20170501.util.CsvBean;
import com.ijiangtao.studevtech.demo.csv.day.DT20170501.util.CsvUtil;

/**
 * 
 * @author ijiangtao
 * @version 2017-05-01 13:54
 */
public class SuperCSVDemo {

	public static void main(String[] args) throws Exception {
		/*读取和写入csv文件*/
		// readCsv();
		/*
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
		String dateTime = format.format(new Date());
		System.out.println(dateTime);
		writeCsv();
		*/

		/*测试CsvUtil*/
		// 获取文件所在的绝对路径
		File f = new File("src\\main\\java\\com\\ijiangtao\\studevtech\\demo\\csv\\day\\DT20170501\\csvfile");
		String absolutePath = f.getAbsolutePath();	
		String[] names={"user"};
		
		List<CsvBean> beans=CsvUtil.CsvReader(absolutePath, names,User.READ_PROCESSOR ,User.class);
		
		String writePath="src\\main\\java\\com\\ijiangtao\\studevtech\\demo\\csv\\day\\DT20170501\\csvfile\\created";
		String writeHeader[] = { "username", "password", "date", "zip", "town","isProcessed" };
		
		//SimpleDateFormat formater = new SimpleDateFormat("dd/MM/yyyy");
		for (int i=0;i<beans.size();i++) {
			User u=(User) beans.get(i);
			u.setIsProcessed(true);
		}
		CsvUtil.CsvWriter(writePath, writeHeader, User.WRITE_PROCESSOR, beans);
		
	}

	
	public static void writeCsv() {
		ICsvBeanWriter writer = null;
		try {

			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
			String dateTime = format.format(new Date());
			System.out.println("dateTime=" + dateTime);

			File path = new File(
					"src\\main\\java\\com\\ijiangtao\\studevtech\\demo\\csv\\day\\DT20170501\\csvfile\\created");
			System.out.println("mkdirs=" + path.mkdirs());
			String absolutePath = path.getAbsolutePath();
			System.out.println("absolutePath=" + absolutePath);

			File file = new File(dateTime + ".csv");
			System.out.println("createNewFile=" + file.createNewFile());

			writer = new CsvBeanWriter(
					new OutputStreamWriter(new FileOutputStream(new File(absolutePath + "\\" + dateTime + ".csv")),
							"UTF-8"),
					CsvPreference.EXCEL_PREFERENCE);

			// 输出头部
			String header[] = { "username", "password", "date", "zip", "town","isProcessed" };
			writer.writeHeader(header);

			// 按顺序输出数据
			UserBean bean = new UserBean();
			bean.setUsername("tom");
			bean.setPassword("tomp");
			SimpleDateFormat formater = new SimpleDateFormat("dd/MM/yyyy");
			String date = formater.format(new Date());
			System.out.println("formater=" + date);
			bean.setDate(date);

			bean.setTown("Beijing");
			bean.setZip(123321);
			
			bean.setProcessed(true);
			writer.write(bean, header, UserBean.write_processor);

			bean.setUsername("may");
			writer.write(bean, header, UserBean.write_processor);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	
	public static void readCsv() {

		// Interface for CSV readers reading into objects/beans.
		ICsvBeanReader inFile = null;

		try {

			// 获取文件所在的绝对路径
			File f = new File("src\\main\\java\\com\\ijiangtao\\studevtech\\demo\\csv\\day\\DT20170501\\csvfile");
			String absolutePath = f.getAbsolutePath();
			System.out.println("absolutePath=" + absolutePath);

			inFile = new CsvBeanReader(new FileReader(absolutePath + "\\user.csv"), CsvPreference.EXCEL_PREFERENCE);
			final String[] header = inFile.getHeader(true);
			UserBean user;
			while ((user = inFile.read(UserBean.class, header, UserBean.processors)) != null) {
				System.out.println(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (inFile != null) {
				try {
					inFile.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
}
